package com.example.a17376080_dlz_finaltest;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class My_Fragment extends Fragment implements View.OnClickListener{
    View view;
    ImageView iv_my;
    TextView tv_username;
    private Button btn_tuichu_login;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.my_fragment,container,false);
        initView();
        return view;
    }

    private void initView() {
        iv_my=view.findViewById(R.id.iv_my);
        iv_my.setOnClickListener(this);
        btn_tuichu_login=view.findViewById(R.id.btn_tuichu_login);
        tv_username=view.findViewById(R.id.tv_username);
        tv_username.setOnClickListener(this);
        btn_tuichu_login.setOnClickListener(this);
    }



    @Override
    public void onStart() {
        super.onStart();
        SharedPreferences sharedPreferences=getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        tv_username.setText(sharedPreferences.getString("currentUsername",""));

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_my:
                Intent intent=new Intent(getActivity(),EnterActivity.class);
                intent.putExtra("tv_username",tv_username.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.btn_tuichu_login:
                SharedPreferences.Editor sp=getActivity().getSharedPreferences("login",getActivity().MODE_PRIVATE).edit();
                sp.putString("currentUsername","");
                sp.putString("currentPassword","");
                sp.apply();
                break;
            default:
                break;
        }
    }
}
